package cn.bone.mqtt.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.MqttHeaders;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.jms.*;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Created by MD-01 on 2017/3/14.
 */
@Service
public class SendService {

    /**测试发送mqtt消息*/
    @Resource(name="mqtt")
    private MqttPahoMessageHandler mqttHandler;

    private Logger logger = LoggerFactory.getLogger("发送方");

    //@Scheduled(fixedDelay=3000)
    public void sendMqtt(){

        logger.info("准备发送mqtt消息");
        org.springframework.messaging.Message<String> message = MessageBuilder.withPayload("--123456--")
                .setHeader(MqttHeaders.TOPIC, "bone_demo_mqtt_topic")
                .build();
        mqttHandler.handleMessage(message);
        logger.info("发送mqtt消息成功");
    }
    /**测试发送mqtt消息end*/


    /**测试发送jms消息*/
    @Resource(name="myTopic")
    private Topic topic;
    @Resource
    private JmsTemplate template;

    @Scheduled(fixedDelay=3000)
    public void sendJms(){

        logger.info("准备发送jms消息");
        template.send(topic, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                return session.createTextMessage("戴泽你好,"+sdf.format(new Date()));
            }
        });
    }
    /**测试发送jms消息end*/
}
